<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 源数据
        let person = {
            name:'张三',
            age:19
        }

        // vue2实现响应式
        /* let p = {}
        Object.defineProperty(p,'name',{
            configurable:true,
            get(){
                return person.name
            },
            set(value){
                person.name = value
                console.log('有人修改了name属性');
            }
        })
        Object.defineProperty(p,'age',{
            get(){
                return person.age
            },
            set(value){
                person.age = value
                console.log('有人修改了age属性');
            }
        }) */

        // vue实现响应式
        const p = new Proxy(person,{
            get(target,propName){
                console.log('有人读取了p',target,propName);
                return Reflect.get(target,propName)
            },
            // 有人修改p的某个属性，或给p追加某个属性是调用
            set(target,propName,value){
                console.log(`有人修改了p身上${propName}属性`,target,propName,value);
                Reflect.set(target,propName,value)
            },
            // 有人删除p的某个属性
            deleteProperty(target,propName){
                console.log(`有人删除了p身上${propName}属性`,target,propName);
                return Reflect.deleteProperty(target,propName)
            }
        }) 

        let obj = {
            a:1,
            b:2
        }
        // 通过Object.defineProperty去操作
        /* Object.defineProperty(obj,'c',{
            get(){
                return 3
            }
        })
        Object.defineProperty(obj,'c',{
            get(){
                return 4
            }
        }) */
        // 通过Reflect.defineProperty去操作
        /* const x1 = Reflect.defineProperty(obj,'c',{
            get(){
                return 3
            }
        })
        console.log(x1); // true
        const x2 =  Reflect.defineProperty(obj,'c',{
            get(){
                return 4
            }
        })
        console.log(x2); //false */
    </script>
</body>
</html>